package com.darren.euc.dao.impl;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.darren.euc.dao.RoleDao;
import com.darren.euc.entity.Role;


@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role, Integer> implements RoleDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getAll() {
		Session session = sessionFactory.getCurrentSession();	
		Query query  = session.createQuery("from Role order by id ");
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getRoleType() {
		Session session = sessionFactory.getCurrentSession();
		Query query  = session.createSQLQuery("select min(id) as id,role_name,role_type,max(ifnull(function_names,'')) as function_names,max(create_time) as create_time from euc_roles group by role_name,role_type order by convert(role_name using gbk) asc").addEntity(Role.class);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getRoleByRoleName(String roleName) {
		Session session = sessionFactory.getCurrentSession();	
		Query query  = session.createQuery("from Role as a where a.roleName = ?");
		query.setParameter(0, roleName);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Role> getRoleByRoleNameExcludeCurrentId(String roleName,Integer roleId){
		Session session = sessionFactory.getCurrentSession();	
		Query query  = session.createQuery("from Role as a where a.roleName = ? and id <> ? ");
		query.setParameter(0, roleName);
		query.setParameter(1, roleId);
		return query.list();
	}
	
	@Override
	public int deleteRoleFunctionByRoleId(Integer roleId){
		Session session = sessionFactory.getCurrentSession();	
		Query query  = session.createSQLQuery("delete from euc_role_function where role_id = ?");
		query.setParameter(0, roleId);
		int rowCount = query.executeUpdate();
		return rowCount;	
	}
	
	
}